package pers.lan.linkedlist;

/**
 * @author <p><a href="mailto:1728209643@qq.com">XiaoSan</a>
 * @date 2019/9/8 11:24 AM
 */
public class LC_203 {
    public static void main(String[] args) {
        System.out.println(
                new LC_203().removeElements(
                        ListNode.build(new int[]{1, 1, 2, 2, 3, 4, 1, 5, 5, 1}),
                        1
                )
        );
    }

    public ListNode removeElements(ListNode head, int val) {
        while (head != null && head.val == val) {
            head = head.next;
        }
        if (head != null) {
            ListNode p = head;
            ListNode q = p.next;
            while (q != null) {
                if (q.val != val) {
                    p.next = q;
                    p = p.next;
                }
                q = q.next;
            }
            p.next = null;
        }
        return head;
    }
}
